<?php
class Mgraficov extends Model{

	private $_numeroSemana = 9;
	
    public function __construct()
    {
 		parent::Model();
 		
 	}
 	
    public function encodeDataURL($strDataURL, $addNoCacheStr=false) 
    {
            if ($addNoCacheStr==true) {
		    if (strpos(strDataURL,"?")<>0)
			  $strDataURL .= "&FCCurrTime=" . Date("H_i_s");
		     else
			   $strDataURL .= "?FCCurrTime=" . Date("H_i_s");
             }

	        return urlencode($strDataURL);
	
    }

    public function datePart($mask, $dateTimeStr) 
    {
             @list($datePt, $timePt) = explode(" ", $dateTimeStr);
             $arDatePt = explode("-", $datePt);
             $dataStr = "";
 
             if (count($arDatePt) == 1) {
                   list($year, $month, $day) = $arDatePt;
        
                     switch ($mask) {
                           case "m": return $month;
                           case "d": return $day;
                           case "y": return $year;
              }
      
               return (trim($month . "/" . $day . "/" . $year));
             }
           return $dataStr;
    }

    public function renderChart($chartSWF, $strURL, $strXML, $chartId, $chartWidth, $chartHeight, $debugMode, $registerWithJS) 
    {

	       if ($strXML=="")
                 $tempData = "//Set the dataURL of the chart\n\t\tchart_$chartId.setDataURL(\"$strURL\")";
           else
                 $tempData = "//Provide entire XML data using dataXML method\n\t\tchart_$chartId.setDataXML(\"$strXML\")";

           $chartIdDiv = $chartId . "Div";
           $ndebugMode = $this->boolToNum($debugMode);
           $nregisterWithJS = $this->boolToNum($registerWithJS);
   
           $render_chart = <<<RENDERCHART

	       <!-- START Script Block for Chart $chartId -->
	       <div id="$chartIdDiv" align="center">
		       Chart.
	        </div>
	        
	       <script type="text/javascript">	
		      //Instantiate the Chart	
		        var chart_$chartId = new FusionCharts("$chartSWF", "$chartId", "$chartWidth", "$chartHeight", "$ndebugMode", "$nregisterWithJS");
		       $tempData
		      //Finally, render the chart.
		       chart_$chartId.render("$chartIdDiv");
	       </script>	
	          <!-- END Script Block for Chart $chartId --> 
RENDERCHART;

              return $render_chart;
}


     public function renderChartHTML($chartSWF, $strURL, $strXML, $chartId, $chartWidth, $chartHeight, $debugMode) {
  
                 $strFlashVars = "&chartWidth=" . $chartWidth . "&chartHeight=" . $chartHeight . "&debugMode=" . $this->boolToNum($debugMode);
                 if ($strXML=="")
     
                 $strFlashVars .= "&dataURL=" . $strURL;
                 else  
                 $strFlashVars .= "&dataXML=" . $strXML;

           $HTML_chart = <<<HTMLCHART
	                     <!-- START Code Block for Chart $chartId -->
	                     <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="$chartWidth" height="$chartHeight" id="$chartId">
		                      <param name="allowScriptAccess" value="always" />
		                      <param name="movie" value="$chartSWF"/>		
		                      <param name="FlashVars" value="$strFlashVars" />
		                      <param name="quality" value="high" />
		                      <embed src="$chartSWF" FlashVars="$strFlashVars" quality="high" width="$chartWidth" height="$chartHeight" name="$chartId" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
	                    </object>
	                    <!-- END Code Block for Chart $chartId -->
HTMLCHART;

  return $HTML_chart;
}


  public function boolToNum($bVal) 
  {
         return (($bVal==true) ? 1 : 0);
  }
  
  public function getSalesByProdCatXML() {
   
	$intYear = 1996;
	$addJSLinks = true;
	$forDataURL= false;
	
	$cadena = $this->ultimo($this->semana(3));
	$cadena = explode('+',$cadena);
	
	$ors['CategoryID'] = 22;

	      $strCat = "<categories>";

	           $strAmtDS = "<dataset seriesname='Inbound'>";
          	    $strQtyDS = "<dataset seriesName='Seguimiento Inbound' parentYAxis='S'>";

          	         for ($i = 0;$i < $this->_numeroSemana; $i++)
          	         {   
          	         	   $elemento = $this->_numeroSemana - 1;
          	         	   $var = $elemento - $i;
          	         	   
          	         	    $semana = date('W')-$var;
          	         	   
          	         	   
          	         		$cadena = $this->ultimo($this->semana($var));
	                        $cadena = explode('+',$cadena);
	                        
	                              $arrayWhereH = array( 'FechaSistema >=' => $cadena[0],
	                                                    'FechaSistema <=' => $cadena[1]);
	                              
	                              $arrayWhereR = array( 'DateSistema >=' => $cadena[0],
	                                                    'DateSistema <=' => $cadena[1]); 
	                        
	                        $cantidad = $this->detallerentradas->LoadDetalleCantidadFiltro($cadena[0],$cadena[1]);
	                        
	                        if ($cantidad == 0)
	                            continue;
	                        
	                        $cantidar = $this->mtransaccionentrada->loadCantByWhere($arrayWhereR);
	                        $cantidah = $this->TransaccionHistorico->loadCantByWhere($arrayWhereH);
	                        
	                        $cantida1 = $cantidar + $cantidah; 
	                        
	                        if ($semana <= 0) {
	                        	$semanatitle = 52 + $semana; 
	                        }else{
	                        	   $semanatitle = $semana;
	                        }
	                     
	                        
	                        
          	         	$strCat .= "<category label='".$semanatitle."'/>";
                        $strAmtDS .= "<set value='".$cantida1."' />";
                        $strQtyDS .= "<set value='".$cantidad."'/>";
          	         }
          	        
         
	                	
                            
	         $strCat .= "</categories>";
	         
	                 
	                     
	                     
	                     
	$strAmtDS .= "</dataset>";
	$strQtyDS .= "</dataset>";

	$strXML2 = $strCat . $strAmtDS . $strQtyDS;
	$strXML  = "<chart caption='Inbound Por Semanas' palette='' animation='' formatNumberScale='0' PYAXisName='Inbound' SYAxisName='Seguimiento Inbound' numberPrefix='' labeldisplay='STAGGER' seriesNameInToolTip='0' showValues='0' plotSpacePercent='0'>";
    $strXML.=$strXML2;
    $strXML .= "<styles><definition><style type='font' name='CaptionFont' size='15' color='' /><style type='font' name='SubCaptionFont' bold='0' /></definition><application><apply toObject='caption' styles='CaptionFont' /><apply toObject='SubCaption' styles='SubCaptionFont' /></application></styles>";
	$strXML .= "</chart>";
	

	return $this->renderChart(base_url()."public/swf_charts/MSColumn3DLineDY.swf", "fdg",$strXML, "CatCum", 750,300, false, false);
	
}
  


}